#!/usr/bin/env python
# coding=utf-8
import argparse
import os.path
from typing import Final

import uvicorn
import llmapi
from llmapi.api import create_app
from llmapi.context import LLMAPIContext
from llmapi.config import LLMAPIConfig
import transformers

def main():
    parser = argparse.ArgumentParser(
        description='Start LLM and Embeddings models as a service.')

    parser.add_argument('--config', required=False,
                        help='Path to an alternative config file. Defaults to config.yaml')
    parser.add_argument('--port', type=int, help='Port number to run the service',
                        default=8080)

    args = parser.parse_args()

    config_file = args.config or llmapi.CONFIG_YAML
    print(f"transformers version is {transformers.__version__}")
    print("> Load config and arguments...")
    print(f"Config file: {config_file}")
    print(f"Port: {args.port}")
    print("begin create app")
    config = LLMAPIConfig.parse_file(config_file)
    ctx = LLMAPIContext(config=config, models=config.load_models())
    app = create_app(ctx)
    print("> Start API server...")
    uvicorn.run(app, host="0.0.0.0", port=args.port)


if __name__ == '__main__':
    main()
